<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class AppUpdateForm extends CFormModel
{
	public $id;
	public $name;
	public $descr;
	public $isEnabled;
	public $key;
	public $secret;

	public $old_id;
	public $old_name;
	public $old_descr;
	public $old_isEnabled;
	public $old_key;
	public $old_secret;

	public $enabled_array = array("1" => '可用',"0" => '禁用');

	public function init(){
	}

	public function rules()
	{
		return array(
		array('name', 'length', 'max'=>255),
		array('descr', 'length', 'max'=>255),
		array('isEnabled', 'numerical', 'integerOnly'=>true),
		);
	}
	public function attributeLabels()
	{
		return array(
        	'name'       => 'APP名称',
         	'key'        => 'APP KEY',
            'secret'     => 'APP SECRET',
            'descr'      => 'APP备注',
            'isEnabled'  => '是否可用',
		);
	}



	
	private function _fillApp($appData){

		$this->name      = $appData->osr_requester_name;
		$this->key       = $appData->osr_consumer_key;
		$this->secret    = $appData->osr_consumer_secret;
		$this->descr     = $appData->osr_application_descr;
		$this->isEnabled = $appData->osr_enabled;

		$this->old_name      = $appData->osr_requester_name;
		$this->old_key       = $appData->osr_consumer_key;
		$this->old_secret    = $appData->osr_consumer_secret;
		$this->old_descr     = $appData->osr_application_descr;
		$this->old_isEnabled = $appData->osr_enabled;
	}

	
	public function initData($id)
	{
		$this->id = $id;
		$model=ServerRegistry::model()->findByPk($id);
		if(!isset($model))
		throw new CHttpException(404,'The requested page does not exist.');
		$this->_fillApp($model);
	}

	
	public function save()
	{
		if($this->validate()){
			if(isset($this->descr) && $this->descr !== $this->old_descr){
				$appMeta=ServerRegistry::model()->findByPk($this->id);
				$appMeta["osr_application_descr"]=$this->descr;
				$appMeta->save();
				unset($appMeta);
			}
			if(isset($this->name) && $this->name !== $this->old_name){
				$appMeta=ServerRegistry::model()->findByPk($this->id);
				$appMeta["osr_requester_name"]=$this->name;
				$appMeta->save();
				unset($appMeta);
			}
			if(isset($this->isEnabled) && $this->isEnabled !== $this->old_isEnabled){
				$appMeta=ServerRegistry::model()->findByPk($this->id);
				$appMeta["osr_enabled"]=$this->isEnabled;
				$appMeta->save();
				unset($appMeta);
			}
			return true;
		}
	}
}